<?php
/**
 * Name: 管理员实现.
 *
 * Author:李强 EdwinLee
 * Company:上海煦鸣网络科技有限公司
 * Time: 2018-01-10 21:25
 */

namespace App\Services\Repo;

use App\Models\User;
use App\Services\Interfaces\RepoServiceInterface;
use App\Services\Service\Helper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserRepo implements RepoServiceInterface
{
    /**
     * 查询数据集合
     */
    public function index(Request $request)
    {
        $query_fields = ['page' => 1, 'limit' => 10, 'opt_type' => '', 'keywords' => '', 'status' => ''];
        $inputs = Helper::getInputs($request->all(), $query_fields);
        $page = intval($inputs['page']);
        $limit = intval($inputs['limit']);
        $start = ($page - 1) * $limit;
        $where = [];
        $where['where'][]=[
            'status','!=',-1
        ];
        if ($inputs['opt_type'] != "" && $inputs['keywords'] != "") {
            $where['where'][] = [$inputs['opt_type'], 'like', '%' . $inputs['keywords'] . '%'];
        }
        if ($inputs['status'] != "") {
            $where['where'][] = ['status', '=', $inputs['status']];
        }
        $result = User::queryAll($where, [], [$start, $limit]);
        $result['conditions'] = $inputs;
        return $result;
    }

    /**
     * 新增数据
     * @param array|null $_options //数据参数
     * @return Model
     */
    public function store(array $_options = null): array
    {
        $result = ['code' => 0, 'message' => '新增失败'];
        $mobile = isset($_options['mobile']) ? $_options['mobile'] : '';
        if ($mobile == '') {
            $result['message'] = '登录手机号码不能为空';
            return $result;
        }
        if (User::isExists(1, $mobile)) {
            $result['message'] = '手机号码已经存在';
            return $result;
        }
        $email = isset($_options['email']) ? $_options['email'] : '';
        if ($email == '') {
            $result['message'] = '邮箱不能为空';
            return $result;
        }
        if (User::isExists(2, $email)) {
            $result['message'] = '邮箱已经存在';
            return $result;
        }
        $password = isset($_options['password']) ? $_options['password'] : '';
        if ($password == '') {
            $result['message'] = '请设置登录密码';
            return $result;
        }
        $datas = [];
        $datas['avatar'] = isset($_options['avatar']) ? $_options['avatar'] : '';
        $datas['nickname'] = isset($_options['nickname']) ? $_options['nickname'] : '';
        $datas['mobile'] = $mobile;
        $datas['email'] = isset($_options['email']) ? $_options['email'] : '';
        $datas['password'] = Hash::make($password);
        $datas['status'] = isset($_options['status']) ? $_options['status'] : 1;
        try {
            if (false !== User::create($datas)) {
                $result['code'] = 1;
                $result['message'] = '新增成功';
            } else {
                $result['message'] = '新增失败1';
            }
        } catch (\Exception $e) {
            $result['message'] = '新增失败:' . $e->getMessage();
        }
        return $result;
    }

    /**
     * 查询数据详情
     * @param array $_filter 过滤条件
     * @return Model
     */
    public function show(array $_filter): Model
    {
        $result = User::find($_filter)->first();
        if ($result) {
            return $result;
        }
    }

    /**
     * 修改数据
     * @param array $_filter 过滤条件
     * @param array $_update_data 要修改的数据
     * @return bool
     */
    public function update(array $_filter, array $_update_data): array
    {
        $result = ['code' => 0, 'message' => '更新失败'];
        $email = isset($_update_data['email']) ? $_update_data['email'] : '';
        if ($email == '') {
            $result['message'] = '邮箱不能为空';
            return $result;
        }
        if (User::isExists(2, $email, $_filter['id'])) {
            $result['message'] = '邮箱已经存在';
            return $result;
        }
        $datas = [];
        $datas['avatar'] = isset($_update_data['avatar']) ? $_update_data['avatar'] : '';
        $datas['nickname'] = isset($_update_data['nickname']) ? $_update_data['nickname'] : '';
        $datas['email'] = isset($_update_data['email']) ? $_update_data['email'] : '';
        try {
            if (false !== User::where($_filter)->update($datas)) {
                $result['code'] = 1;
                $result['message'] = '更新成功';
            } else {
                $result['message'] = '更新失败1';
            }
        } catch (\Exception $e) {
            $result['message'] = '更新失败:' . $e->getMessage();
        }
        return $result;
    }

    /**
     * 删除数据
     * @param array $_filter 过滤条件
     * @return bool
     */
    public function destroy(array $_filter): array
    {
        $result = ['code' => 0, 'message' => '删除失败'];
        $admin = User::where($_filter)->first();
        if (is_null($admin)) {
            $result['message'] = '管理员不存在';
        } else {
            if ($admin->is_sys == 1) {
                $result['message'] = '超级管理员不能被删除';
            } else {
                $admin->status = -1;
                if (false !== $admin->save()) {
                    $result['code'] = 1;
                    $result['message'] = '删除成功';
                }
            }
        }
        return $result;
    }
}